<?php
/**
 * @date 2012-4-8
 * @author GhostWei
 */
class controller_admin_system_region extends system_controller_action {
	
	private $user = null;
	private $referer = null;
	private $language = null;
	public function init() {
		if($this->adminLogin()) {
			return;
		}
		$this->view->language = $this->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
		$this->view->referer = $this->referer = $this->getRequest()->getHeader('referer');
				
		$page = new system_nav_page();
		$page->setLabel($this->language->load('System'))->setRoute('system/index');
		$this->crumbs()->addPage($page);
		
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Region'))->setRoute('system/region/index');
		$this->crumbs()->addPage($page);
	}
	
	public function action_index() {
		$this->redirector()->gotoRouteAndExit('system/region/province');
		$this->render();
	}
	
	public function action_province() {
		$concept = new common_region_concept();
		$this->view->list = $concept->fetchAll();
		
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_editProvince() {
		$id = intval($this->getRequest()->getParam('id'));		
		$concept = new common_region_concept();
		if(!($row = $concept->fetch(array('region.id = ?' => $id)))) {
			throw new common_exception('Province is Not existing.');
		}
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Edit') . ':' . $row->getName())->setRoute('system/region/editProvince')->setParams(array('id' => $id));
		$this->_provinceRender($row, $page);
	}
	
	public function action_newProvince() {
		$concept = new common_region_concept();
		$row = $concept->create();
		$page = new system_nav_page();
		$page->setLabel($this->language->load('New Province'))->setRoute('system/region/newProvince');
		$this->_provinceRender($row, $page);
	}
	
	private function _provinceRender($row, $lpage) {
		if($this->getRequest()->isPost()) {
			$this->referer = $this->getRequest()->getPost('referer');
			try {
				if($this->getRequest()->getPost('delete')) {
					$row->delete();
					$this->redirector()->gotoRouteAndExit('system/region/index');
				}
				$row->setName(trim($this->getRequest()->getPost('name')));
				$row->setEname(trim($this->getRequest()->getPost('ename')));
				$row->setZipcode(trim($this->getRequest()->getPost('zipcode')));
				$row->save();
				$this->view->throwMessage = 'Complete!';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$this->crumbs()->addPage($lpage);
		$this->render();
	}
	
	public function action_city() {
		$province_id = intval($this->getRequest()->getParam('province_id'));
		$concept = new common_region_concept();
		if(!($province = $concept->fetch(array('region.id = ?' => $province_id)))) {
			throw new common_exception("This Province is not Exsiting.");
		}
		
		$concept = new common_region_city_concept();
		$this->view->list = $concept->fetchAll(array('city.province_id = ?' => $province_id));
		$this->view->province = $province;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($province->getName())->setRoute('system/region/city')->setParams(array('province_id' => $province_id));
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_editCity() {
		$id = intval($this->getRequest()->getParam('id'));		
		$concept = new common_region_city_concept();
		if(!($row = $concept->fetch(array('city.id = ?' => $id)))) {
			throw new common_exception('City is Not existing.');
		}
		$this->view->province = $row->getProvince();
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Edit') . ': ' . $row->getName())->setRoute('system/region/editCity')->setParams(array('id' => $id));
		$this->_cityRender($row, $page);
	}
	
	public function action_newCity() {
		$province_id = intval($this->getRequest()->getParam('province_id'));		
		$concept = new common_region_concept();
		if(!($province = $concept->fetch(array('region.id = ?' => $province_id)))) {
			throw new common_exception('Province is Not existing.');
		}
		$this->view->province = $province;
		$concept = new common_region_city_concept();
		$row = $concept->create();
		$row->setProvinceId($province->getId());
		$row->setProvinceName($province->getName());
		$page = new system_nav_page();
		$page->setLabel($this->language->load('New City'))->setRoute('system/region/newCity')->setParams(array('province_id' => $province_id));
		$this->_cityRender($row, $page);
	}
	
	private function _cityRender($row, $lpage) {
		if($this->getRequest()->isPost()) {
			$this->referer = $this->getRequest()->getPost('referer');
			try {
				if($this->getRequest()->getPost('delete')) {
					$row->delete();
					$this->redirector()->gotoRouteAndExit('system/region/city', array('province_id' => $row->getProvinceId()));
				}
				$row->setName(trim($this->getRequest()->getPost('name')));
				$row->setEname(trim($this->getRequest()->getPost('ename')));
				$row->setZipcode(trim($this->getRequest()->getPost('zipcode')));
				$row->save();
				$this->view->throwMessage = 'Complete!';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($row->getProvinceName())->setRoute('system/region/city')->setParams(array('province_id' => $row->getProvinceId()));
		$this->crumbs()->addPage($page);
		$this->crumbs()->addPage($lpage);
		$this->render();
	}

	public function action_zone() {
		$city_id = intval($this->getRequest()->getParam('city_id'));
		$concept = new common_region_city_concept();
		if(!($city = $concept->fetch(array('city.id = ?' => $city_id)))) {
			throw new common_exception("This City is not Exsiting.");
		}
		
		$concept = new common_region_zone_concept();
		$this->view->list = $concept->fetchAll(array('zone.city_id = ?' => $city_id));
		$this->view->city = $city;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($city->getProvinceName())->setRoute('system/region/city')->setParams(array('province_id' => $city->getProvinceId()));
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($city->getName())->setRoute('system/region/zone')->setParams(array('city_id' => $city->getId()));
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_editZone() {
		$id = intval($this->getRequest()->getParam('id'));		
		$concept = new common_region_zone_concept();
		if(!($row = $concept->fetch(array('zone.id = ?' => $id)))) {
			throw new common_exception('Zone is Not existing.');
		}
		$this->view->city = $row->getCity();
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Edit') . ': ' . $row->getName())->setRoute('system/region/editZone')->setParams(array('id' => $id));
		$this->_zoneRender($row, $page);
	}
	
	public function action_newZone() {
		$city_id = intval($this->getRequest()->getParam('city_id'));		
		$concept = new common_region_city_concept();
		if(!($city = $concept->fetch(array('city.id = ?' => $city_id)))) {
			throw new common_exception('City is Not existing.');
		}
		$this->view->city = $city;
		$concept = new common_region_zone_concept();
		$row = $concept->create();
		$row->setProvinceId($city->getProvinceId());
		$row->setProvinceName($city->getProvinceName());
		$row->setCityId($city->getId());
		$row->setCityName($city->getName());
		$page = new system_nav_page();
		$page->setLabel($this->language->load('New Zone'))->setRoute('system/region/newZone')->setParams(array('city_id' => $city_id));
		$this->_zoneRender($row, $page);
	}
	
	private function _zoneRender($row, $lpage) {
		if($this->getRequest()->isPost()) {
			$this->referer = $this->getRequest()->getPost('referer');
			try {
				if($this->getRequest()->getPost('delete')) {
					$row->delete();
					$this->redirector()->gotoRouteAndExit('system/region/zone', array('city_id' => $row->getCityId()));
				}
				$row->setName(trim($this->getRequest()->getPost('name')));
				$row->setEname(trim($this->getRequest()->getPost('ename')));
				$row->setZipcode(trim($this->getRequest()->getPost('zipcode')));
				$row->setAreacode(trim($this->getRequest()->getPost('areacode')));
				$row->save();
				$this->view->throwMessage = 'Complete!';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Province'))->setRoute('system/region/province');
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($row->getProvinceName())->setRoute('system/region/city')->setParams(array('province_id' => $row->getProvinceId()));
		$this->crumbs()->addPage($page);
		$page = new system_nav_page();
		$page->setLabel($row->getCityName())->setRoute('system/region/zone')->setParams(array('city_id' => $row->getCityId()));
		$this->crumbs()->addPage($page);
		$this->crumbs()->addPage($lpage);
		$this->render();
	}
	
}